import { useState, useEffect } from 'react';
import { IHttpHooksProps } from '@/interface';
import { Http } from '@/utils';

export default function useHttpHook({
  url,
  method = 'GET',
  headers = {},
  data = {},
  params = {},
  watch = [],
}: IHttpHooksProps) {
  const [result, setResult] = useState<any>();
  const [loading, setLoading] = useState<boolean>(true);
  useEffect(() => {
    Http({
      url,
      method,
      headers,
      data,
      params,
      setResult,
      setLoading,
    });
  }, watch);

  if (result && result.data) {
    return [result.data, loading];
  }
  return [result, loading];
}
